<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Export\OrderyExporter;
use App\Admin\Models\Goods;
use App\Admin\Models\Ordery;
use Illuminate\Http\Request;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use App\Admin\Models\Users;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Storage;
use function foo\func;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
class OrderyController extends AdminController
{
    protected $title = '异常订单';

    protected function grid()
    {
        $grid = new Grid(new Ordery);
        $auth = Auth::guard('admin')->user()->toArray();//获取后端登录用户信息
        if($auth['account']){
            $level = Users::where('account',$auth['account'])->value('level');
            $uid = Users::where('account',$auth['account'])->value('id');
            if($level == 1 || $level == 2){ //判断用户的身份  是否是经销商或者代理 1 代理 2经销商
                //查询当前用户下的所有用户
                $array = DB::table('user_relation')->where('pid',$uid)->where('lay','<=','2')->pluck('user_id')->toArray();
                $grid->model()->whereIn('uid',$array)->where('is_normal','1')->orderBy('id','desc');
            }
        }else{
            $grid->model()->where('is_normal','1')->orderBy('id','desc');
        }

        $grid->disableCreateButton();
//        $grid->disableActions();
        $grid->actions(function ($actions) {
            $actions->disableDelete();
//            $actions->disableView();
            $actions->disableEdit();
        });
        $grid->filter(function($filter){
            $filter->disableIdFilter();

            $filter->column(1/3, function ($filter) {
                $filter->equal('order_sn', '订单编号');
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('ad_name', '开卡人');
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('ad_phone', '预留手机');
            });
            $filter->column(1/3,function ($filter){
                $filter->equal('agent', '代理商')
                    ->select(Users::where('level','1')
                        ->pluck('realname','id'));
            });
//            $filter->column(1/3, function ($filter) {
//                $filter->equal('source','订单来源')->select([
//                    '1'  => '公众号',
//                    '2'  => '天猫商城',
//                    '3'  => '京东商城',
//                    '4'  => '拼多多'
//                ]);
//            });
//            $filter->column(1/3, function ($filter) {
//                $filter->equal('status','订单状态')->select([
//                    '-1' => '取消单',
////                    '0'  => '已下单',
//                    '1'  => '待激活',
//                    '2'  => '已完成'
//                ]);
//            });

//            $filter->column(1/3, function ($filter) {
//                $filter->between('active_time', '激活时间')->datetime();
//            });
        });
        $grid->number('序号');
        $grid->rows(function ($row, $number) {
            $page = request('page');
            if($page){
                $page = ($page-1)*10;
            }
            $row->column('number', $page +$number+1);
        });
        $grid->column('order_sn', '订单编号');
//        $grid->column('order_sn', '订单编号')->expand( function ($model) {
//            $comments = $model->where('order_sn',$this->order_sn)->get()->map(function ($comment) {
//                return $comment->only(['ad_name', 'ad_phone', 'ad_detail','ad_card_img','ad_card_imgs','ad_image']);
//            });
//            return new Table(['开卡人姓名', '开卡人手机号', '开卡人详细地址','身份证正面照','身份证反面照','开卡人免冠照'], $comments->toArray());
//        });
        $grid->column('goods_id','商品名称')->display(function ($goods_id){
            return Goods::where('id',$goods_id)->value('name');
        });
        $grid->column('uid','购买用户')->display(function ($uid){
            return Users::whereId($uid)->value('realname');
        });
        $grid->column('agent','代理商名称')->display(function ($agent){
            return Users::whereId($agent)->value('realname');
        });
//        $grid->column('preat','上级用户')->display(function (){
//            $id = $this->uid;
//            $pid = DB::table('user_relation')->where('user_id',$id)->where('lay','1')->value('pid');
//            return DB::table('users')->whereId($pid)->value('realname');
//        });
        $grid->column('cancel_reason','订单异常原因')->display(function ($cancel_reason){
            return mb_substr($cancel_reason,'0','15').'...';
        });
//        $grid->column('status','订单状态')->using([
//            '-1' => '取消',
//            '0' => '已下单',
//            '1' => '待激活',
//            '2' => '已完成'
//        ])->dot([
//            '-1' => 'danger',
//            '0' => 'primary',
//            '1' => 'info',
//            '2' => 'success'
//        ]);
        $grid->column('source','订单来源')->display(function ($source){
            switch ($source){
                case 1:
                    return '公众号';
                    break;
                case 2:
                    return '天猫商城';
                    break;
                case 3:
                    return '京东商城';
                    break;
                case 4:
                    return '拼多多';
                    break;
            }
        });
        $grid->column('created_at','订单创建时间');
//        $grid->column('update_at','激活时间');
        $grid->column('ad_name','开卡人');
        $grid->column('ad_phone','预留电话');
        $grid->exporter(new OrderyExporter());
        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(Ordery::findOrFail($id));
        $show->panel()->tools(function ($tools){
            $tools->disableDelete();
            $tools->disableEdit();
        });
        $show->field('order_sn','订单编号');
        $show->field('uid','用户名')->as(function ($uid){
            return Users::whereId($uid)->value('nickname');
        });
        $show->field('phone','预留手机号');
        $show->field('ad_name','开户姓名');
        $show->field('ad_phone','开户手机号');
        $show->field('ad_detail','开户地址');
        $show->field('ad_city','开户区域');
        $show->field('ad_detail','开户地址');
        $show->field('ad_card','身份证号');
        $show->field('ad_card_img','开户身份正面照')->image(env("APP_URL"), '200', '200');
        $show->field('ad_card_imgs','开户身份正面照')->image(env("APP_URL"), '200', '200');
        $show->field('ad_image','开户免冠照')->image(env("APP_URL"), '200', '200');
        $show->field('cancel_reason','异常原因');
        return $show;
    }
}
